package de.jmda.gen.java.impl;

import static de.jmda.util.StringUtil.sb;
import de.jmda.gen.GeneratorException;
import de.jmda.gen.impl.DefaultGenerator;
import de.jmda.gen.java.MethodNameGenerator;
import de.jmda.util.StringUtil;

public class DefaultMethodNameGenerator
    extends DefaultGenerator implements MethodNameGenerator
{
	public final static String DEFAULT_METHOD_NAME = "method_name";

	public DefaultMethodNameGenerator()
	{
		super();
	}

	public DefaultMethodNameGenerator(String input)
	{
		super(input);
	}

	@Override
	public StringBuffer generate() throws GeneratorException
	{
		StringBuffer result = super.generate();

		if (StringUtil.isBlank(result))
		{
			result = sb(DEFAULT_METHOD_NAME);
		}

		return validate(result);
	}

	protected StringBuffer validate(StringBuffer stringBuffer)
			throws GeneratorException
	{
		if (false == StringUtil.startsWithLower(stringBuffer))
		{
			throw new GeneratorException(
					stringBuffer + " has to start with lower case");
		}

		return stringBuffer;
	}

	@Override
  public void setMethodName(String methodName)
  {
		setOutput(methodName);
  }

	@Override
  public String getMethodName() throws GeneratorException
  {
	  return generate().toString();
  }
}